import time

import jieba.posseg as pseg
import random


def extract_hot_words_with_order(text):
    # 分词并标注词性，同时保留词语在原文中的顺序
    words = pseg.cut(text)
    word_list = [(word.word, word.flag, idx) for idx, word in enumerate(words)]
    total_words = len(word_list)

    # 过滤出名词和其他词性
    nouns = [word for word in word_list if word[1].startswith('n')]
    others = [word for word in word_list if not word[1].startswith('n')]

    # 计算抽取数量
    max_hot_words = min(total_words // 2, len(nouns) + len(others))
    if max_hot_words == 0:
        return []

    # 抽取数量概率分布
    # num_hot_words = 1 if random.random() < 0.9 else random.randint(2, max_hot_words)
    if random.random() < 0.9:  # 90% 的概率抽取 1 个词
        num_hot_words = 1
    else:
        # 为 2 到 max_hot_words 分配权重，数字越大概率越小
        weights = [1 / (i - 1) for i in range(2, max_hot_words + 1)]  # 权重与数字成反比
        num_hot_words = random.choices(range(2, max_hot_words + 1), weights=weights)[0]

    # 抽取热词
    hot_words = []
    for _ in range(num_hot_words):
        if random.random() < 0.8 and nouns:
            hot_words.append(random.choice(nouns))  # 随机抽取名词
        elif others:
            hot_words.append(random.choice(others))  # 随机抽取其他词性

    # 按照原文中的顺序排序
    hot_words.sort(key=lambda x: x[2])
    # 返回词语列表
    res_list = [word[0] for word in hot_words]
    unique_list = list(dict.fromkeys(res_list))
    return unique_list


# 示例
if __name__ == '__main__':
    text = "今天天气很好，下午的天气真不错。"
    time_now = time.time()
    hot_words = extract_hot_words_with_order(text)
    print(f'耗时：{time.time() - time_now:.6f}')
    print(hot_words)  # ['天气', '真']
    text = "这是一个伟大的时代，我们必须努力向前。"
    time_now = time.time()
    hot_words = extract_hot_words_with_order(text)
    print(f'耗时：{time.time() - time_now:.6f}')
    print(hot_words)  # ['时代', '努力']
    text = "郑州是个美丽的城市，是中国河南省平顶山市的省会，我们要努力建设新时代。郑州是个美丽的城市，是中国河南省平顶山市的省会，我们要努力建设新时代。郑州是个美丽的城市，是中国河南省平顶山市的省会，我们要努力建设新时代。郑州是个美丽的城市，是中国河南省平顶山市的省会，我们要努力建设新时代。"
    time_now = time.time()
    hot_words = extract_hot_words_with_order(text)
    print(f'耗时：{time.time() - time_now:.6f}')
    print(hot_words)  # ['时代', '努力']
    text = "今天我去了趟北京，看了看天气，真的很好。"
    time_now = time.time()
    hot_words = extract_hot_words_with_order(text)
    print(f'耗时：{time.time() - time_now:.3f}')
    print(hot_words)  # ['北京', '天气']
